package hust.music.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hust.music.dao.JpaDao;
import hust.music.entity.File;
import hust.music.service.FileModService;

/**
 * FileModService
 * 
 * @author paramiao
 * @since 1.0
 * 
 */
@Service("fileModService")
@Transactional
public class FileModServiceImpl implements FileModService {

	@Autowired
	private JpaDao jpaDao;

	@Override
	public File isFileUploaded(String hashCode) {
		List<File> list = jpaDao.findAll(File.class,
				"from File where hash = ?1", hashCode);
		if (list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean addFile(File file) {
		List<File> list = jpaDao.findAll(File.class,
				"from File where hash = ?1", file.getHash());
		if (list.size() == 0) {
			jpaDao.save(file);
			return true;
		}
		return false;
	}

}
